package hollycrm.com.libuip.sip;

import android.util.Log;

import org.pjsip.pjsua2.Account;
import org.pjsip.pjsua2.AccountConfig;
import org.pjsip.pjsua2.BuddyConfig;
import org.pjsip.pjsua2.OnIncomingCallParam;
import org.pjsip.pjsua2.OnInstantMessageParam;
import org.pjsip.pjsua2.OnRegStateParam;
import org.pjsip.pjsua2.pjsip_status_code;

import java.util.ArrayList;

/**
 * Created by aber on 2016/12/6.
 *
 * Accounts provide identity (or identities)
 * of the user who is currently using the
 * application. An account has one SIP
 * Uniform Resource Identifier (URI) associated
 * with it. In SIP terms, this URI acts as Address
 * of Record (AOR) of the person and is used
 * as the From header in outgoing requests.Account
 * may or may not have client registration associated
 * with it. An account is also associated with
 * route set and some authentication credentials,
 * which are used when sending SIP request messages
 * using the account. An account also has presence
 * status, which will be reported to remote peer
 * when they subscribe to the account’s presence,
 * or which is published to a presence server if
 * presence publication is enabled for the account.
 *
 * At least one account MUST be created in the
 * application, since any outgoing requests
 * require an account context. If no user
 * association is required, application can
 * create a userless account by calling
 * Account.create(). A userless account
 * identifies local endpoint instead
 * of a particular user, and it corresponds to
 * a particular transport ID.
 *
 * Also one account must be set as the default
 * account, which will be used as the account
 * identity when pjsua fails to match incoming
 * request with any accounts using the stricter
 * matching rules.
 */
public class UIPAccount extends Account {
    private static final String TAG = UIPAccount.class.getSimpleName();

    public ArrayList<UIPBuddy> buddyList = new ArrayList<>();
    public AccountConfig cfg;

    public UIPAccount(AccountConfig config) {
        super();
        this.cfg = config;
    }

    public UIPBuddy addBuddy(BuddyConfig bud_cfg) {
        /* Create Buddy */
        UIPBuddy bud = new UIPBuddy(bud_cfg);
        try {
            bud.create(this, bud_cfg);
        } catch (Exception e) {
            bud.delete();
            bud = null;
        }
        if (bud != null) {
            buddyList.add(bud);
            if (bud_cfg.getSubscribe())
                try {
                    bud.subscribePresence(true);
                } catch (Exception e) {}
        }
        return bud;
    }

    @Override
    public void onRegState(OnRegStateParam prm) {
        if (SipApp.observer != null)
            SipApp.observer.notifyRegState(prm.getCode(),prm.getReason(),prm.getExpiration());
    }

    @Override
    public void onIncomingCall(OnIncomingCallParam prm) {
        Log.d(TAG,"========= Incoming call ========= ");
        UIPCall call = new UIPCall(this, prm.getCallId());
        if (SipApp.observer != null)
            SipApp.observer.notifyIncomingCall(call);
    }

    @Override
    public void onInstantMessage(OnInstantMessageParam prm) {
        //super.onInstantMessage(prm);
        // no op
        //throw new UnsupportedOperationException("not support instance message");
        Log.d(TAG,"======== Incoming pager ========");
        Log.d(TAG,"From     :"+prm.getFromUri());
        Log.d(TAG,"To     :"+prm.getToUri());
        Log.d(TAG,"Contact     :"+prm.getContactUri());
        Log.d(TAG,"Mimetype     :"+prm.getContactUri());
        Log.d(TAG,"Body     :"+prm.getMsgBody());
    }

}
